# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: linchuan

import json
import requests

from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo


def gen_var_ticks(min, max):
  price_list = [1e9, 1000000., 500000., 100000., 50000., 10000., 5000., 1000., 100., 10., 1., 0]
  tick_list = [1000., 500., 100., 50., 10., 5., 1., 0.1, 0.01, 0.001, 0.0001]

  range_list = list(zip(price_list[:-1], price_list[1:]))
  result = []
  for r, tick in zip(range_list, tick_list):
    if r[1] < min:
      continue
    if r[1] > max:
      continue
    assert r[0] > r[1]
    info = {
        'from': r[1],
        'to': r[0],
        'value': tick,
    }
    result.append([info['from'], info['to'], info['value']])
  return result


def gen_min_amount(quote):
  if quote in ['KRW']:
    return float(5000)
  else:
    raise f"unknown quote: %s" % quote


def generate():
  qty_ticksize_dict = {
      'btc_krw': '0.00000001',
      'eth_krw': '0.000001',
      'bch_krw': '0.000001',
      'xrp_krw': '0.01',
      'bsv_krw': '0.000001',
      'ltc_krw': '0.0001',
      'link_krw': '0.0001',
      'eos_krw': '0.0001',
      'trx_krw': '1',
      'zil_krw': '1',
      'etc_krw': '0.0001',
      'xlm_krw': '1',
      'omg_krw': '0.0001',
      'qtum_krw': '0.0001',
      'usdc_krw': '0.0001',
      'mkr_krw': '0.000001',
      'aergo_krw': '1',
      'bnb_krw': '0.0001',
      'dai_krw': '0.0001',
      'bat_krw': '0.01',
      'med_krw': '1',
      'fet_krw': '1',
      'fil_krw': '0.0001',
      'bcha_krw': '0.0001',
      'wbtc_krw': '0.00000001',
      'ada_krw': '0.01',
      'btc_usdc': '0.00000001',
      'uni_krw': '0.0001',
      'yfi_krw': '0.00000001',
      'dot_krw': '0.0001',
      'algo_krw': '0.0001',
      'enj_krw': '0.00000001',
      'xtz_krw': '0.0001',
      'sushi_krw': '0.0001',
      'aave_krw': '0.000001',
      'snx_krw': '0.0001',
      'matic_krw': '0.01',
      'flow_krw': '0.0001',
      '1inch_krw': '0.0001',
      'ftt_krw': '0.0001',
      'band_krw': '0.0001',
      'luna_krw': '0.0001',
      'sol_krw': '0.0001',
      'klay_krw': '0.0001',
      'grt_krw': '0.01',
      "atom_krw": '0.0001',
      "amp_krw": '0.001',
      "chz_krw": '0.01',
      "mina_krw": '0.0001',
      "comp_krw": '0.000001',
      "axs_krw": '0.0001',
      "audio_krw": '0.0001',
      "crv_krw": '0.0001',
      "theta_krw": '0.0001',
      "rly_krw": '0.01',
      "nu_krw": '0.01',
      "ocean_krw": '0.0001',
      "storj_krw": '0.0001',
      "doge_krw": '0.01',
      "vet_krw": '0.01',
      "lrc_krw": '0.01',
      "alpha_krw": '0.0001',
      "uma_krw": '0.0001',
      "sand_krw": '0.01',
      "icp_krw": '0.0001',
      "hnt_krw": '0.0001',
      "btt_krw": '0.001',
      "xec_krw": '0.001',
      "sgb_krw": '0.01',
      "rari_krw": '0.0001',
      "wemix_krw": '0.0001',
      "dydx_krw": '0.0001',
      "shib_krw": '0.0001',
      "inj_krw": '0.0001',
      "rune_krw": '0.0001',
      "hbar_krw": '0.01',
      "stx_krw": '0.0001',
      "avax_krw": '0.000001',
      "ksm_krw": '0.000001',
      "egld_krw": '0.000001',
      "ens_krw": '0.0001',
      "mana_krw": '0.0001',
      "ohm_krw": '0.0001',
      "celo_krw": '0.0001',
      "lpt_krw": '0.0001',
      "tdrop_krw": '0.0001',
      "ron_krw": '0.0001',
      "gala_krw": '0.0001',
      "ftm_krw": '0.0001',
      "bit_krw": '0.00000001',
      "imx_krw":  '0.1',
      "near_krw": '0.00000001',
      "gtc_krw": '0.01',
      "ren_krw": '0.0000001',
      "ar_krw": '0.000000000001',
      "kda_krw": '0.00000001',
      "ape_krw": '0.00000001',
      "one_krw": '0.00000001',
      "ctx_krw": '0.000001',
      "looks_krw": '0.000000000000000001',
      "ygg_krw": '0.0001',
      "cvx_krw": '0.00000001',
      "ksp_krw": '0.00000001',
      "ilv_krw": '0.00000001',
      "bora_krw": '0.00000001',
      "osmo_krw": '0.000001',
      "srm_krw": '0.00000001',
      "perp_krw": '0.000000000000000001',
      "rose_krw": '0.00000001',
      "npt_krw": '0.0001',
      "waxp_krw": '0.001',
      "rare_krw": '0.001',
      "gmt_krw": '0.00000001',
      "op_krw": '0.00000001',
      "ldo_krw": '0.00000001',
      "bal_krw": '0.00000001',
      "gno_krw": '0.000001',
      "index_krw": '0.00000001',
      "syn_krw": '0.00000001',
      "gmx_krw": '0.00000001',
      "xpla_krw": '0.00000001',
      "rpl_krw": '0.00000001',
      "pokt_krw": '0.000001',
      "apt_krw": '0.00000001',
      "waxl_krw": '0.000001',
      "cfg_krw": '0.01',
      "arb_krw": '0.000000000000000001',
      "sui_krw": '0.001',
      "gal_krw": '0.00000001',
      "pepe_krw": '0.001',
      "blur_krw": '0.000000000000000001',
      "mnt_krw":'0.00000000000001',
      "wld_krw": '0.00000001',
      "sei_krw": '0.00000001',
  }

  mea = 'Spot.Korbit.v1'
  me = '.'.join(mea.split('.')[:2])
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  data = requests.get("https://api.korbit.co.kr/v1/constants").json()['exchange']

  pis = []
  for symbol, info in data.items():
    if not isinstance(info, dict):
      continue

    native_symbol = symbol
    native_base = symbol.split('_')[0]
    native_quote = symbol.split('_')[1]
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)
    symbol = "%s-%s" % (base, quote)
    if native_symbol in qty_ticksize_dict:
      qty_ticksize = float(qty_ticksize_dict[native_symbol])
    else:
      print('Please add qty ticksize for korbit symbol', native_symbol)
      qty_ticksize = 0.00000001

    if info['order_max_size'] == 0:
      info['order_max_size'] = 10000000

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        price_ticksize=gen_var_ticks(0, 1e9),
        qty_ticksize=[[
            float(info['order_min_size']),
            float(info['order_max_size']),
            qty_ticksize]],
        min_amount=gen_min_amount(quote),
    )

    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


def gen_coin1_constants():
  data = generate()
  data = convert_coin1_constants(data)
  return data


def convert_coin1_constants(data):
  base_currency_list = [info['native_base'].upper() for info in data]
  res = {
      'base_currency_list': list(set(base_currency_list)),
  }
  return res


if __name__ == "__main__":
  print(json.dumps(generate(), indent=2))
